home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / Crypto / PublicKey / RSA.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  9.9 KB  |  291 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __revision__ = '$Id: RSA.py,v 1.20 2004/05/06 12:52:54 akuchling Exp $'
  5. from Crypto.PublicKey import pubkey
  6. from Crypto.Util import number
  7.  
  8. try:
  9.     from Crypto.PublicKey import _fastmath
  10. except ImportError:
  11.     _fastmath = None
  12.  
  13.  
  14. class error(Exception):
  15.     pass
  16.  
  17.  
  18. def generate(bits, randfunc, progress_func = None):
  19.     """generate(bits:int, randfunc:callable, progress_func:callable)
  20.  
  21.     Generate an RSA key of length 'bits', using 'randfunc' to get
  22.     random data and 'progress_func', if present, to display
  23.     the progress of the key generation.
  24.     """
  25.     obj = RSAobj()
  26.     if progress_func:
  27.         progress_func('p,q\n')
  28.     
  29.     p = q = 0x1L
  30.     while number.size(p * q) < bits:
  31.         p = pubkey.getPrime(bits / 2, randfunc)
  32.         q = pubkey.getPrime(bits / 2, randfunc)
  33.     if p > q:
  34.         p = q
  35.         q = p
  36.     
  37.     obj.p = p
  38.     obj.q = q
  39.     if progress_func:
  40.         progress_func('u\n')
  41.     
  42.     obj.u = pubkey.inverse(obj.p, obj.q)
  43.     obj.n = obj.p * obj.q
  44.     obj.e = 0x10001L
  45.     if progress_func:
  46.         progress_func('d\n')
  47.     
  48.     obj.d = pubkey.inverse(obj.e, (obj.p - 1) * (obj.q - 1))
  49.     if not bits <= 1 + obj.size():
  50.         raise AssertionError, 'Generated key is too small'
  51.     return obj
  52.  
  53.  
  54. def construct(tuple):
  55.     '''construct(tuple:(long,) : RSAobj
  56.     Construct an RSA object from a 2-, 3-, 5-, or 6-tuple of numbers.
  57.     '''
  58.     obj = RSAobj()
  59.     if len(tuple) not in (2, 3, 5, 6):
  60.         raise error, 'argument for construct() wrong length'
  61.     len(tuple) not in (2, 3, 5, 6)
  62.     for i in range(len(tuple)):
  63.         field = obj.keydata[i]
  64.         setattr(obj, field, tuple[i])
  65.     
  66.     if len(tuple) >= 5:
  67.         if obj.p > obj.q:
  68.             obj.p = obj.q
  69.             obj.q = obj.p
  70.         
  71.     
  72.     if len(tuple) == 5:
  73.         obj.u = pubkey.inverse(obj.p, obj.q)
  74.     
  75.     return obj
  76.  
  77.  
  78. class RSAobj(pubkey.pubkey):
  79.     keydata = [
  80.         'n',
  81.         'e',
  82.         'd',
  83.         'p',
  84.         'q',
  85.         'u']
  86.     
  87.     def _encrypt(self, plaintext, K = ''):
  88.         if self.n <= plaintext:
  89.             raise error, 'Plaintext too large'
  90.         self.n <= plaintext
  91.         return (pow(plaintext, self.e, self.n),)
  92.  
  93.     
  94.     def _decrypt(self, ciphertext):
  95.         if not hasattr(self, 'd'):
  96.             raise error, 'Private key not available in this object'
  97.         hasattr(self, 'd')
  98.         if self.n <= ciphertext[0]:
  99.             raise error, 'Ciphertext too large'
  100.         self.n <= ciphertext[0]
  101.         return pow(ciphertext[0], self.d, self.n)
  102.  
  103.     
  104.     def _sign(self, M, K = ''):
  105.         return (self._decrypt((M,)),)
  106.  
  107.     
  108.     def _verify(self, M, sig):
  109.         m2 = self._encrypt(sig[0])
  110.         if m2[0] == M:
  111.             return 1
  112.         return 0
  113.  
  114.     
  115.     def _blind(self, M, B):
  116.         tmp = pow(B, self.e, self.n)
  117.         return M * tmp % self.n
  118.  
  119.     
  120.     def _unblind(self, M, B):
  121.         tmp = pubkey.inverse(B, self.n)
  122.         return M * tmp % self.n
  123.  
  124.     
  125.     def can_blind(self):
  126.         '''can_blind() : bool
  127.         Return a Boolean value recording whether this algorithm can
  128.         blind data.  (This does not imply that this
  129.         particular key object has the private information required to
  130.         to blind a message.)
  131.         '''
  132.         return 1
  133.  
  134.     
  135.     def size(self):
  136.         '''size() : int
  137.         Return the maximum number of bits that can be handled by this key.
  138.         '''
  139.         return number.size(self.n) - 1
  140.  
  141.     
  142.     def has_private(self):
  143.         '''has_private() : bool
  144.         Return a Boolean denoting whether the object contains
  145.         private components.
  146.         '''
  147.         if hasattr(self, 'd'):
  148.             return 1
  149.         return 0
  150.  
  151.     
  152.     def publickey(self):
  153.         '''publickey(): RSAobj
  154.         Return a new key object containing only the public key information.
  155.         '''
  156.         return construct((self.n, self.e))
  157.  
  158.  
  159.  
  160. class RSAobj_c(pubkey.pubkey):
  161.     keydata = [
  162.         'n',
  163.         'e',
  164.         'd',
  165.         'p',
  166.         'q',
  167.         'u']
  168.     
  169.     def __init__(self, key):
  170.         self.key = key
  171.  
  172.     
  173.     def __getattr__(self, attr):
  174.         if attr in self.keydata:
  175.             return getattr(self.key, attr)
  176.         if self.__dict__.has_key(attr):
  177.             self.__dict__[attr]
  178.         else:
  179.             raise AttributeError, '%s instance has no attribute %s' % (self.__class__, attr)
  180.         return attr in self.keydata
  181.  
  182.     
  183.     def __getstate__(self):
  184.         d = { }
  185.         for k in self.keydata:
  186.             if hasattr(self.key, k):
  187.                 d[k] = getattr(self.key, k)
  188.                 continue
  189.         
  190.         return d
  191.  
  192.     
  193.     def __setstate__(self, state):
  194.         n = state['n']
  195.         e = state['e']
  196.         if not state.has_key('d'):
  197.             self.key = _fastmath.rsa_construct(n, e)
  198.         else:
  199.             d = state['d']
  200.             if not state.has_key('q'):
  201.                 self.key = _fastmath.rsa_construct(n, e, d)
  202.             else:
  203.                 p = state['p']
  204.                 q = state['q']
  205.                 u = state['u']
  206.                 self.key = _fastmath.rsa_construct(n, e, d, p, q, u)
  207.  
  208.     
  209.     def _encrypt(self, plain, K):
  210.         return (self.key._encrypt(plain),)
  211.  
  212.     
  213.     def _decrypt(self, cipher):
  214.         return self.key._decrypt(cipher[0])
  215.  
  216.     
  217.     def _sign(self, M, K):
  218.         return (self.key._sign(M),)
  219.  
  220.     
  221.     def _verify(self, M, sig):
  222.         return self.key._verify(M, sig[0])
  223.  
  224.     
  225.     def _blind(self, M, B):
  226.         return self.key._blind(M, B)
  227.  
  228.     
  229.     def _unblind(self, M, B):
  230.         return self.key._unblind(M, B)
  231.  
  232.     
  233.     def can_blind(self):
  234.         return 1
  235.  
  236.     
  237.     def size(self):
  238.         return self.key.size()
  239.  
  240.     
  241.     def has_private(self):
  242.         return self.key.has_private()
  243.  
  244.     
  245.     def publickey(self):
  246.         return construct_c((self.key.n, self.key.e))
  247.  
  248.  
  249.  
  250. def generate_c(bits, randfunc, progress_func = None):
  251.     if progress_func:
  252.         progress_func('p,q\n')
  253.     
  254.     p = q = 0x1L
  255.     while number.size(p * q) < bits:
  256.         p = pubkey.getPrime(bits / 2, randfunc)
  257.         q = pubkey.getPrime(bits / 2, randfunc)
  258.     if p > q:
  259.         p = q
  260.         q = p
  261.     
  262.     if progress_func:
  263.         progress_func('u\n')
  264.     
  265.     u = pubkey.inverse(p, q)
  266.     n = p * q
  267.     e = 0x10001L
  268.     if progress_func:
  269.         progress_func('d\n')
  270.     
  271.     d = pubkey.inverse(e, (p - 1) * (q - 1))
  272.     key = _fastmath.rsa_construct(n, e, d, p, q, u)
  273.     obj = RSAobj_c(key)
  274.     if not bits <= 1 + obj.size():
  275.         raise AssertionError, 'Generated key is too small'
  276.     return obj
  277.  
  278.  
  279. def construct_c(tuple):
  280.     key = apply(_fastmath.rsa_construct, tuple)
  281.     return RSAobj_c(key)
  282.  
  283. object = RSAobj
  284. generate_py = generate
  285. construct_py = construct
  286. if _fastmath:
  287.     generate = generate_c
  288.     construct = construct_c
  289.     error = _fastmath.error
  290.  
  291.